import { getInsertSql } from "../util/mysql";
import { prismaClient } from "../util/prisma";
import { I_roleInfo, I_roleAllInfo } from "../util/someInterface";
import { timeFormat } from "../util/util";
import { roleMysql } from "./roleInfo";
import { svr_info } from "./svr_info";


export class LoginUtil {

    getAllRoleInfo(uid: number, cb: (err: any, allInfo: I_roleAllInfo) => void) {
        this.getRoleInfo(uid, (err, role) => {
            if (err) {
                return cb(err, null as any);
            }
            cb(null, { "role": role });
        });
    }

    private async getRoleInfo(uid: number, cb: (err: any, info: I_roleInfo) => void) {
        const player = await prismaClient.player.findUnique({
            where: { uid: uid }
        })
        if (!player) {
            this.createRole(uid, cb);
            return
        }

        // cb(false, )
        // "uid": number,              // uid
        // "nickname": string,         // 昵称
        // "gold": number,           // 金币
        // "regTime": string,           // 注册时间
        // "loginTime": string,           // 最近登录时间
        // "gameInfo": { "win": number, "all": number }, // 胜利局数，总局数
        let info = JSON.parse(player.gameInfo ?? '')
        let roleInfo: I_roleInfo = {
            uid: player.uid,
            nickname: player.nickname ?? '',
            gold: player.gold ?? 0,
            regTime: timeFormat(player.regTime ?? 0),
            loginTime: timeFormat(player.loginTime ?? 0),
            gameInfo: {
                win: info.win,
                all: info.all
            },
        }
        cb(null, roleInfo);
        // roleInfo.regTime = timeFormat(roleInfo.regTime);
        // roleInfo.loginTime = timeFormat(roleInfo.loginTime);

        // svr_info.mysql.query(`select * from player where uid = ${uid} limit 1`, null, (err, res: I_roleInfo[]) => {
        //     if (err) {
        //         return cb(err, null as any);
        //     }
        //     if (res.length === 0) {
        //         this.createRole(uid, cb);
        //     } else {
        //         let roleInfo = res[0]
        //         roleInfo.regTime = timeFormat(roleInfo.regTime);
        //         roleInfo.loginTime = timeFormat(roleInfo.loginTime);
        //         cb(null, roleInfo);
        //     }
        // });
    }

    private async createRole(uid: number, cb: (err: any, info: I_roleInfo) => void) {
        let nowTime = timeFormat(new Date());
        let roleInfo: I_roleInfo = {
            "uid": uid,
            "nickname": "农民" + uid,
            "gold": 1000,
            "regTime": nowTime,
            "loginTime": nowTime,
            "gameInfo": { "all": 0, "win": 0 },
        }

        await prismaClient.player.create({
            data: {
                "uid": uid,
                "nickname": "农民" + uid,
                "gold": 1000,
                "regTime": new Date(),
                "loginTime": new Date(),
                "gameInfo": JSON.stringify({ "all": 0, "win": 0 }),
            }
        })

        cb(null, roleInfo);
        // svr_info.mysql.query(getInsertSql("player", roleInfo), null, (err) => {
        //     if (err) {
        //         return cb(err, null as any);
        //     }
        //     cb(null, roleInfo);
        // });
    }
}